package com.gonsin.gview.logic.services.data;

import com.gonsin.gview.annotations.GContext;
import com.gonsin.gview.logic.model.DataRequest;
import com.gonsin.gview.model.struct.EnumItem;
import com.gonsin.i18n.logic.T;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * json语句处理器
 */
public class GEnumsDataManager extends GDataManager{

    private GContext context;

    private String dataId;

    private List<EnumItem> result;

    public GEnumsDataManager(GContext context, String dataId, List<EnumItem> enumItems) {
        super(GEnumsDataManager.class.getName(), enumItems.toString());
        this.context = context;
        this.dataId = dataId;
        this.result = enumItems;
    }

    @Override
    public Object generalData(HttpServletRequest request, HttpServletResponse response, DataRequest dataRequest) {
        // 针对I18N做转换
        List<EnumItem> list = copyList(result);
        for(EnumItem item : list){
            if(item == null){
                continue;
            }
            String title = item.getTitle();
            if(title != null){
                title = T.t(title);
                item.setTitle(title);
            }
        }
        return list;
    }

    private List<EnumItem> copyList(List<EnumItem> list) {
        List<EnumItem> listNew = new ArrayList<>();
        for(EnumItem item : list){
            EnumItem newItem = new EnumItem();
            newItem.setTitle(item.getTitle());
            newItem.setKey(item.getKey());
            newItem.setValue(item.getValue());
            newItem.setParentKey(item.getParentKey());
            listNew.add(newItem);
        }
        return listNew;
    }
}
